﻿
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using Model;


namespace MsDAL
{
    public partial class Sys0a05DAL : Sys0a05Base
    {
        /// <summary>
        /// 根据人员或角色的信息得到模块信息,权限列表
        /// </summary>
        /// <param name="rolecode">角色信息</param>
        /// <param name="usercode">用户信息</param>
        /// <returns></returns>
        public DataTable GetAllPermission(string rolecode, string usercode)
        {
            string sql = "select a.*,ISNULL(dbo.fun_mkact(a.modulecode,@rolecode,@usercode),'0') AS act,"
                   + " ISNULL((SELECT TOP 1 1 FROM Sys0a05 WHERE modulecode =a.modulecode "
                   + " AND rolecode = @rolecode OR usercode = @usercode ),0) as flg "
                   + " from Sys0a03 a"
                   + " ORDER BY a.sortorder ASC ";

            SqlParameter p = new SqlParameter("@usercode", usercode);
            SqlParameter p1 = new SqlParameter("@rolecode", rolecode);

            return DBHelper.GetData(sql, p, p1);
        }


        /// <summary>
        /// 得到某个用户的所有权限
        /// </summary>
        /// <param name="usercode">用户信息</param>
        /// <returns></returns>
        public DataTable GetUserPermission(string usercode)
        {
            string sql = " SELECT DISTINCT Z.modulecode,Z.modulename, Z.parentcode,Z.sortorder,Z.plist,Z.purl,Z.isroot,Z.haschild,Z.pdact "
                + " FROM "
                    + " ( "
                    + " SELECT DISTINCT B.modulecode,B.modulename, B.parentcode,B.sortorder,B.plist,B.purl,B.pdact, "
                    + " (SELECT CASE WHEN B.parentcode IS NULL OR  B.parentcode = '' THEN 1 ELSE 0 END) isroot, "
                    + " ISNULL((SELECT TOP 1 1 FROM Sys0a03 X WHERE X.parentcode =A.modulecode),0) haschild "
                    + " FROM Sys0a05 A "
                    + " INNER JOIN Sys0a03 B ON A.modulecode = B.modulecode AND A.actioncode = 'V'"
                    + " WHERE dbo.NVL(usercode,'XXX') = dbo.NVL(@usercode,'XXX') "
                    + " UNION "
                    + " SELECT DISTINCT B.modulecode,B.modulename, B.parentcode,B.sortorder,B.plist,B.purl, B.pdact,"
                    + " (SELECT CASE WHEN B.parentcode IS NULL OR  B.parentcode = '' THEN 1 ELSE 0 END) isroot, "
                    + " ISNULL((SELECT TOP 1 1 FROM Sys0a03 X WHERE X.parentcode =A.modulecode),0) haschild "
                    + " FROM Sys0a05 A "
                    + " INNER JOIN Sys0a03 B ON A.modulecode = B.modulecode AND A.actioncode = 'V'"
                    + " WHERE EXISTS (SELECT 1 FROM Sys0a02 Z "
                    + "	WHERE dbo.NVL(A.rolecode,'XXX') = dbo.NVL(Z.rolecode,'XXX') "
                    + "	AND usercode = @usercode "
                    + "	)"
                  + " ) Z ORDER BY Z.sortorder ASC";

            SqlParameter p = new SqlParameter("@usercode", usercode);
            return DBHelper.GetData(sql, p);
        }

        /// <summary>
        /// 得到某个用户某个模块拥有的相关权限
        /// </summary>
        /// <param name="usercode">用户信息</param>
        /// <param name="moduleName">模块名称</param>
        /// <returns></returns>
        public DataTable GetModulePermission(string usercode, string moduleName)
        {
            string sql = "SELECT DISTINCT actioncode "
                + " FROM Sys0a05 A WHERE dbo.NVL(usercode,'XXX') = dbo.NVL(@usercode,'XXX') "
                + " AND lower(A.modulecode) = lower(@modulecode) "
                + " UNION "
                + " SELECT DISTINCT actioncode "
                + " FROM Sys0a05 A "
                + " WHERE EXISTS (SELECT 1 FROM Sys0a02 B "
                + "	WHERE dbo.NVL(A.rolecode,'XXX') = dbo.NVL(B.rolecode,'XXX') "
                + "	AND usercode = @usercode "
                + " AND lower(A.modulecode) = lower(@modulecode) "
                + "	)";

            SqlParameter p = new SqlParameter("@usercode", usercode);
            SqlParameter p1 = new SqlParameter("@modulecode", moduleName);

            return DBHelper.GetData(sql, p, p1);
        }

        public SqlApp DeleteByRolecode(string rolecode)
        {
            string sql = "DELETE FROM Sys0a05 WHERE rolecode =@rolecode";

            SqlParameter[] parameters = {
				new SqlParameter("@rolecode",SqlDbType.NVarChar)
				};
            parameters[0].Value = rolecode;

            return new SqlApp(sql, new ArrayList(parameters), true, "");
        }


        public SqlApp DeleteByUsercode(string usercode)
        {
            string sql = "DELETE FROM Sys0a05 WHERE usercode =@usercode";

            SqlParameter[] parameters = {
				new SqlParameter("@usercode",SqlDbType.NVarChar)
				};
            parameters[0].Value = usercode;

            return new SqlApp(sql, new ArrayList(parameters), true, "");
        }
    }
}
